package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Awards;
import com.ruoyi.system.domain.CheckDetail;
import com.ruoyi.system.domain.Project;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CheckListMapper;
import com.ruoyi.system.domain.CheckList;
import com.ruoyi.system.service.ICheckListService;

/**
 * 审核Service业务层处理
 *
 * @author Edward
 * @date 2024-04-16
 */
@Service
public class CheckListServiceImpl implements ICheckListService {
    @Autowired
    private CheckListMapper checkListMapper;
    @Autowired
    private DemandServiceImpl demandServiceImpl;

    /**
     * 查询审核
     *
     * @param id 审核主键
     * @return 审核
     */
    @Override
    public CheckList selectCheckListById(Long id) {
        return checkListMapper.selectCheckListById(id);
    }

    /**
     * 查询审核列表
     *
     * @param checkList 审核
     * @return 审核
     */
    @Override
    public List<CheckList> selectCheckListList(CheckList checkList) {
        return checkListMapper.selectCheckListList(checkList);
    }

    /**
     * 新增审核
     *
     * @param checkList 审核
     * @return 结果
     */
    @Override
    public int insertCheckList(CheckList checkList) {
        checkList.setCreateTime(DateUtils.getNowDate());
        return checkListMapper.insertCheckList(checkList);
    }

    /**
     * 修改审核
     *
     * @param checkList 审核
     * @return 结果
     */
    @Override
    public int updateCheckList(CheckList checkList) {
        return checkListMapper.updateCheckList(checkList);
    }

    /**
     * 批量删除审核
     *
     * @param ids 需要删除的审核主键
     * @return 结果
     */
    @Override
    public int deleteCheckListByIds(Long[] ids) {
        return checkListMapper.deleteCheckListByIds(ids);
    }

    /**
     * 删除审核信息
     *
     * @param id 审核主键
     * @return 结果
     */
    @Override
    public int deleteCheckListById(Long id) {
        return checkListMapper.deleteCheckListById(id);
    }

    /*
     * type:0:成员 1：需求 2：课题 3：成果 4，认领需求 5:成员加入团队
     * */
    @Override
    public String jointString(String name, Integer type) {
        if (type == 0) {
            return "关于（" + name + "）成为开发队员的申请审核";
        } else if (type == 1) {
            return "关于《" + name + "》需求收集的申请审核";
        } else if (type == 2) {
            return "关于《" + name + "》在研课题的申请审核";
        } else if (type == 3) {
            return "关于《" + name + "》成果的发布审核";
        } else if (type == 4) {
            return "关于《" + name + "》需求的的认领审核";
        } else if (type == 5) {
            return "关于（" + name + "）加入团队的审核";
        } else if (type == 6) {
            return "关于《" + name + "》荣誉的审核";
        } else
            return "无名";
    }

    @Override
    public void addLeaderCheck(Long projectId, Long userId, int type, String projectName) {
        Project project = demandServiceImpl.selectProjectById(projectId);
        String name1 = project.getName();
        CheckList check_l = new CheckList();
        check_l.setProjectId(projectId);
        check_l.setState(0);
        check_l.setUserId(userId);
        check_l.setType(type);
        if (type == 5) {
            check_l.setCheckerId(userId);
            check_l.setName("队长关于（" + projectName + "）加入《" + name1 + "》的审核");
        }

        //新增时间
        check_l.setCreateTime(new Date());

        //添加进审核单
        checkListMapper.insertCheckList(check_l);

    }


    /**
     * 添加审核
     * @param projectId 项目ID
     * @param userId  用户ID
     * @param type  类型
     * @param projectName  项目名称
     */
    @Override
    public void addTwoCheck(Long projectId, Long userId, int type, String projectName) {

        Project project = demandServiceImpl.selectProjectById(projectId);
        String name1 = project.getName();
        CheckList check_a = new CheckList();
        CheckList check_b = new CheckList();
        check_a.setProjectId(projectId);
        check_a.setState(0);
        check_b.setProjectId(projectId);
        check_b.setState(0);
        check_a.setUserId(userId);
        check_b.setUserId(userId);
        //需增加功能
        //0:成员 1：需求 2：课题 3：成果 4，认领需求 5:成员加入团队,6:奖项
        check_a.setType(type);
        check_b.setType(type);
        if (type == 6) {
            //殷悦
            check_a.setCheckerId(37027168L);
            //魏敏
            check_b.setCheckerId(37032279L);
        } else {
            //胡昊
            check_a.setCheckerId(1000043642L);
            //吴兆发
            check_b.setCheckerId(1000068884L);
        }
        //设置名字
        check_a.setName(jointString(projectName, type));
        check_b.setName(jointString(projectName, type));

        if (type == 5) {
            check_a.setName("关于（" + projectName + "）加入《" + name1 + "》的审核");
            check_b.setName("关于（" + projectName + "）加入《" + name1 + "》的审核");
        }
        //新增时间
        check_a.setCreateTime(new Date());
        check_b.setCreateTime(new Date());
        //添加进审核单
        checkListMapper.insertCheckList(check_a);
        checkListMapper.insertCheckList(check_b);

    }

    @Override
    public List<CheckList> determineProject(CheckList check) {
        return checkListMapper.determineProject(check);
    }

    @Override
    public CheckList seeCheck(CheckList check) {
        CheckList checkList = checkListMapper.seeCheck(check);
        return checkList;

    }

    @Override
    public List<CheckList> selectCheckListByCheckerId(Long userId) {
        return checkListMapper.selectCheckListByCheckerId(userId);
    }


    /**
     * 添加审核奖项
     */
    @Override
    public void addCheckAwards(Awards awards) {
        CheckList checkList = new CheckList();
        checkList.setUserId(awards.getUserId());
        checkList.setCheckerId(awards.getChecker1());
        checkList.setName(jointString(awards.getName(), 6));
        checkList.setState(0);
        checkList.setProjectId(awards.getId());
        checkList.setType(6);
        checkList.setDescription(awards.getDescription());
        checkList.setCreateTime(new Date());
        checkListMapper.insertCheckList(checkList);
        checkList.setCheckerId(awards.getChecker2());
        checkListMapper.insertCheckList(checkList);
    }

    @Override
    public CheckList seeAwardsCheck(CheckList check) {
        CheckList checkList = checkListMapper.seeAwardsCheck(check);
        return checkList;

    }

    @Override
    public void updateOtherCheckList(Long projectId) {
        checkListMapper.updateOtherCheckList(projectId);
    }

    @Override
    public void updateOtherCheck(Map<String, Object> map) {
        checkListMapper.updateOtherCheck(map);
    }

    @Override
    public CheckList seeClaimCheck(CheckList check) {
        return checkListMapper.seeClaimCheck(check);
    }

    //type 1 :月度  2 年度
    @Override
    public int checkDeptPoints(long awardsId, int type, int points, Long deptId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("points", points);
        map.put("dept_id", deptId);
        map.put("awards_id", awardsId);
        if (type == 1) {
            return checkListMapper.checkDeptPointsMonth(map);
        } else {
            return checkListMapper.checkDeptPointsYear(map);
        }
    }

    @Override
    public int checkUserPoints(long awardsId, int points, Long userId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("points", points);
        map.put("user_id", userId);
        map.put("awards_id", awardsId);
        return checkListMapper.checkUserPoints(map);
    }


}
